﻿using System.Collections.Generic;
using System.Windows;
using FireStarterModeller.BO;
using FireStarterModeller.Logic.Commands;
using FireStarterModeller.UI.DetailsView;
using FireStarterModeller.UI.DetailsView.Controller;
using Habanero.BO;
using ICommand=FireStarterModeller.Logic.Commands.ICommand;

namespace FireStarterModeller.WPF
{
    /// <summary>
    /// Interaction logic for Program.xaml
    /// </summary>
    public partial class Program : Window
    {
        private TrivialCommandRunner _commandRunner;

        public Program()
        {
            InitializeComponent();
        }

        public void Setup()
        {

            SolutionViewModel model = SolutionViewModel.BuildFrom(Broker.GetBusinessObjectCollection<DMSolution>("")[0]);
            _commandRunner = new TrivialCommandRunner();
            SolutionViewController solutionViewController = new SolutionViewController(new CommandFactory(), _commandRunner, model);
            _solutionView.Init(solutionViewController, model);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Setup();
        }

        private class TrivialCommandRunner : ICommandRunner
        {
            Stack<ICommand> _commands = new Stack<ICommand>();
            Stack<ICommand> _undoneCommands = new Stack<ICommand>();
            public void ApplyCommand(ICommand command) { 
                command.Apply();  
                _commands.Push(command);
                _undoneCommands.Clear();
            }

            public void Undo() {
                if (_commands.Count == 0) return;
                ICommand command = _commands.Pop();
                command.Revert();
                _undoneCommands.Push(command);
            }

            public void Redo()
            {
                if (_undoneCommands.Count == 0) return;
                ICommand command = _undoneCommands.Pop();
                command.Apply();
                _commands.Push(command);
                
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            _commandRunner.Undo();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            _commandRunner.Redo();
        }
    }

    

    
}
